package com.azusa.xbiao.controller;

import com.azusa.xbiao.entity.*;
import com.azusa.xbiao.service.CartService;
import com.azusa.xbiao.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加商品到用户购物车上
     * @param goods
     * @return
     */
    @RequestMapping("insertToCart")
    public Result insertGoodsToCart(@RequestBody Goods goods){
        User loginUser = LoginUserUtil.getLoginUser();
        cartService.insertGoodsToCart(goods,loginUser.getcId());
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 初始化购物车数据
     * @return
     */
    @RequestMapping("initCart")
    public Result initCart(){
        return new Result(StatusCode.SUCCESS,cartService.initCart(LoginUserUtil.getLoginUser().getcId(),0));
    }

    /**
     * 初始化待支付数据
     * @return
     */
    @RequestMapping("initPayCart")
    public Result initPayCart(){
        return new Result(StatusCode.SUCCESS,cartService.initCart(LoginUserUtil.getLoginUser().getcId(),1));
    }

    /**
     * 结算单个商品
     * @param cart
     * @return
     */
    @RequestMapping("settle")
    public Result settle(@RequestBody Cart cart){
        cartService.settle(cart);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 结算购物车中全部商品
     * @param carts
     * @return
     */
    @PutMapping("settleAll")
    public Result settleAll(@RequestBody List<Cart> carts){
        cartService.settleAll(carts);
        return new Result(StatusCode.SUCCESS);
    }

    @PutMapping("deleteGoods")
    public Result deleteGoods(@RequestBody Cart cart){
        cartService.delete(cart);
        return new Result(StatusCode.SUCCESS);
    }
}
